<?php
// +----------------------------------------------------------------------
// | sfadmin - Suifeng Administration
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://suifeng.tech All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: suifeng <1787585212@qq.com>
// +----------------------------------------------------------------------

namespace suifeng\builder\form;

use suifeng\helper\BaseObject;
use suifeng\helper\StringHelper;

abstract class Validator extends BaseObject
{
    /**
     * 验证控件。
     *
     * @var unknown
     */
    public $control;

    /**
     * 错误信息。
     *
     * @var string
     */
    public $message;

    /**
     * 验证错误。
     *
     * @var array
     */
    public $errors;

    /**
     * 验证方法。
     */
    public final function validate()
    {
        $this->errors = array();
        $this->_validate_core();
    }

    /**
     * 验证核心方法。
     */
    protected abstract function _validate_core();

    /**
     * 是否有效？
     *
     * @return boolean
     */
    public final function is_valid()
    {
        $this->validate();
        return empty($this->errors);
    }

    /**
     * 格式化错误信息。
     *
     * @param unknown $message
     * @param unknown $context
     * @return unknown
     */
    protected function _format_message($message, $context = array())
    {
        if ($message == null) {
            $message = $this->message;
        }

        $context = array_merge(get_object_vars($this), $context);
        if ($this->control) {
            $context['control'] = get_object_vars($this->control);
        }

        return StringHelper::replace_token($message, $context);
    }

    /**
     * 获取控件值。
     *
     * @return unknown
     */
    protected function _get_control_value()
    {
        if ($this->control) {
            return $this->control->value;
        } else {
            return null;
        }
    }

    /**
     * 获取控件对应的字段信息。
     *
     * @return array
     */
    public function _get_control_field()
    {
        if ($this->control) {
            return $this->control->_get_field_info();
        } else {
            return [];
        }
    }

    /**
     * 工厂方法。
     *
     * @param unknown $type
     * @param string $attributes
     * @throws \Exception
     * @return mixed
     */
    public static function create($type, $config = array())
    {
        if (is_array($type)) {
            $config = $type;
            $type = $config['rule'];
            unset($config['rule']);
        }

        $class_name = '\\suifeng\\builder\\form\\validator\\' . implode('', array_map('ucfirst', explode('_', $type)));
        if (!class_exists($class_name)) {
            throw new \Exception('Validator type not exists: ' . $type);
        }

        $obj = new $class_name($config);
        return $obj;
    }
}
